package com.anji.tug;

import java.awt.EventQueue;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;

import javax.swing.JTextField;

public class TugMonitor {
	
//	private int objectiveCount;
	private ArrayList<Objective> objectives;

	private JFrame frame;
	private JTextField[] txtCurrentGoal;
	private JTextField[] txtCurrentAverage;
	private JTextField[] txtRecencyAverage;
	private JTextField[] txtRecord;
	private JTextField[] txtFinalGoal;
	private JCheckBox[] chkbxIsOn;
	private JLabel[] lblName;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TugMonitor window = new TugMonitor();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	private ArrayList<Objective> hashToArray(HashMap<String, Objective> map) {
		ArrayList<Objective> array = new ArrayList<Objective>(map.size());
		for (Entry<String, Objective> entry : map.entrySet()) {
			array.add(entry.getValue());
		}
		return array;
	}

	public TugMonitor(HashMap<String, Objective> map) {
		objectives = hashToArray(map);
		initialize();
	}
	
	/**
	 * Create the application.
	 */
	public TugMonitor() {
		initialize();
	}
	

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setTitle("Targetting Unachieved Goals Monitor");
		frame.setBounds(100, 100, 920, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] {200, 130, 130, 130, 110, 130, 50};
		gridBagLayout.rowHeights = new int[]{0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
//		gridBagLayout.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		frame.getContentPane().setLayout(gridBagLayout);
		
		JLabel lblCurrentGoal = new JLabel("Current Goal");
		GridBagConstraints gbc_lblCurrentGoal = new GridBagConstraints();
		gbc_lblCurrentGoal.anchor = GridBagConstraints.WEST;
		gbc_lblCurrentGoal.insets = new Insets(0, 0, 5, 5);
		gbc_lblCurrentGoal.gridx = 1;
		gbc_lblCurrentGoal.gridy = 0;
		frame.getContentPane().add(lblCurrentGoal, gbc_lblCurrentGoal);
		
		JLabel lblCurrentAverage = new JLabel("Current Average");
		GridBagConstraints gbc_lblCurrentAverage = new GridBagConstraints();
		gbc_lblCurrentAverage.anchor = GridBagConstraints.WEST;
		gbc_lblCurrentAverage.insets = new Insets(0, 0, 5, 5);
		gbc_lblCurrentAverage.gridx = 2;
		gbc_lblCurrentAverage.gridy = 0;
		frame.getContentPane().add(lblCurrentAverage, gbc_lblCurrentAverage);
		
		JLabel lblRecencyAverage = new JLabel("Recency Average");
		GridBagConstraints gbc_lblRecencyAverage = new GridBagConstraints();
		gbc_lblRecencyAverage.anchor = GridBagConstraints.WEST;
		gbc_lblRecencyAverage.insets = new Insets(0, 0, 5, 5);
		gbc_lblRecencyAverage.gridx = 3;
		gbc_lblRecencyAverage.gridy = 0;
		frame.getContentPane().add(lblRecencyAverage, gbc_lblRecencyAverage);
		
		JLabel lblRecord = new JLabel("Record");
		GridBagConstraints gbc_lblRecord = new GridBagConstraints();
		gbc_lblRecord.anchor = GridBagConstraints.WEST;
		gbc_lblRecord.insets = new Insets(0, 0, 5, 0);
		gbc_lblRecord.gridx = 4;
		gbc_lblRecord.gridy = 0;
		frame.getContentPane().add(lblRecord, gbc_lblRecord);
		
		JLabel lblFinalGoal = new JLabel("Final Goal");
		GridBagConstraints gbc_lblFinalGoal = new GridBagConstraints();
		gbc_lblFinalGoal.anchor = GridBagConstraints.WEST;
		gbc_lblFinalGoal.insets = new Insets(0, 0, 5, 0);
		gbc_lblFinalGoal.gridx = 5;
		gbc_lblFinalGoal.gridy = 0;
		frame.getContentPane().add(lblFinalGoal, gbc_lblFinalGoal);
		
		JLabel lblIsOn = new JLabel("Is On?");
		GridBagConstraints gbc_lblIsOn = new GridBagConstraints();
		gbc_lblIsOn.anchor = GridBagConstraints.WEST;
		gbc_lblIsOn.insets = new Insets(0, 0, 5, 0);
		gbc_lblIsOn.gridx = 6;
		gbc_lblIsOn.gridy = 0;
		frame.getContentPane().add(lblIsOn, gbc_lblIsOn);
		
//		JLabel lblObjective = new JLabel("Objective1");
//		GridBagConstraints gbc_lblObjective = new GridBagConstraints();
//		gbc_lblObjective.fill = GridBagConstraints.HORIZONTAL;
//		gbc_lblObjective.insets = new Insets(0, 0, 0, 5);
//		gbc_lblObjective.gridx = 0;
//		gbc_lblObjective.gridy = 1;
//		frame.getContentPane().add(lblObjective, gbc_lblObjective);
//		
//		txtCurrentGoal_1 = new JTextField();
//		txtCurrentGoal_1.setEditable(false);
//		GridBagConstraints gbc_txtCurrentGoal_1 = new GridBagConstraints();
//		gbc_txtCurrentGoal_1.fill = GridBagConstraints.HORIZONTAL;
//		gbc_txtCurrentGoal_1.insets = new Insets(0, 0, 0, 5);
//		gbc_txtCurrentGoal_1.gridx = 1;
//		gbc_txtCurrentGoal_1.gridy = 1;
//		frame.getContentPane().add(txtCurrentGoal_1, gbc_txtCurrentGoal_1);
//		txtCurrentGoal_1.setColumns(10);
//		
//		txtCurrentAverage_1 = new JTextField();
//		txtCurrentAverage_1.setEditable(false);
//		GridBagConstraints gbc_txtCurrentAverage_1 = new GridBagConstraints();
//		gbc_txtCurrentAverage_1.fill = GridBagConstraints.HORIZONTAL;
//		gbc_txtCurrentAverage_1.insets = new Insets(0, 0, 0, 5);
//		gbc_txtCurrentAverage_1.gridx = 2;
//		gbc_txtCurrentAverage_1.gridy = 1;
//		frame.getContentPane().add(txtCurrentAverage_1, gbc_txtCurrentAverage_1);
//		txtCurrentAverage_1.setColumns(10);
//		
//		txtRecencyAverage_1 = new JTextField();
//		txtRecencyAverage_1.setEditable(false);
//		GridBagConstraints gbc_txtRecencyAverage_1 = new GridBagConstraints();
//		gbc_txtRecencyAverage_1.fill = GridBagConstraints.HORIZONTAL;
//		gbc_txtRecencyAverage_1.insets = new Insets(0, 0, 0, 5);
//		gbc_txtRecencyAverage_1.gridx = 3;
//		gbc_txtRecencyAverage_1.gridy = 1;
//		frame.getContentPane().add(txtRecencyAverage_1, gbc_txtRecencyAverage_1);
//		txtRecencyAverage_1.setColumns(10);
//		
//		txtFinalGoal_1 = new JTextField();
//		txtFinalGoal_1.setEditable(false);
//		GridBagConstraints gbc_txtFinalGoal_1 = new GridBagConstraints();
//		gbc_txtFinalGoal_1.fill = GridBagConstraints.HORIZONTAL;
//		gbc_txtFinalGoal_1.gridx = 4;
//		gbc_txtFinalGoal_1.gridy = 1;
//		frame.getContentPane().add(txtFinalGoal_1, gbc_txtFinalGoal_1);
//		txtFinalGoal_1.setColumns(10);
		
		txtCurrentGoal = new JTextField[objectives.size()];
		txtCurrentAverage = new JTextField[objectives.size()];
		txtRecencyAverage = new JTextField[objectives.size()];
		txtRecord = new JTextField[objectives.size()];
		txtFinalGoal = new JTextField[objectives.size()];
		lblName = new JLabel[objectives.size()];
		chkbxIsOn = new JCheckBox[objectives.size()];
		
		for (int i = 0; i < objectives.size(); i++) {
			Objective objective = objectives.get(i);
			
			lblName[i] = new JLabel(objective.name);
			GridBagConstraints gbc_lblObjective = new GridBagConstraints();
			gbc_lblObjective.fill = GridBagConstraints.HORIZONTAL;
			gbc_lblObjective.insets = new Insets(0, 0, 0, 5);
			gbc_lblObjective.gridx = 0;
			gbc_lblObjective.gridy = i+1;
			frame.getContentPane().add(lblName[i], gbc_lblObjective);
			
			txtCurrentGoal[i] = new JTextField();
			txtCurrentGoal[i].setEditable(false);
			txtCurrentGoal[i].setText(String.format("%1.3f", objective.goal));
			GridBagConstraints gbc_txtCurrentGoal = new GridBagConstraints();
			gbc_txtCurrentGoal.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtCurrentGoal.insets = new Insets(0, 0, 0, 5);
			gbc_txtCurrentGoal.gridx = 1;
			gbc_txtCurrentGoal.gridy = i+1;
			frame.getContentPane().add(txtCurrentGoal[i], gbc_txtCurrentGoal);
			txtCurrentGoal[i].setColumns(10);
			
			txtCurrentAverage[i] = new JTextField();
			txtCurrentAverage[i].setEditable(false);
			txtCurrentAverage[i].setText(String.format("%1.3f", objective.avg));
			GridBagConstraints gbc_txtCurrentAverage = new GridBagConstraints();
			gbc_txtCurrentAverage.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtCurrentAverage.insets = new Insets(0, 0, 0, 5);
			gbc_txtCurrentAverage.gridx = 2;
			gbc_txtCurrentAverage.gridy = i+1;
			frame.getContentPane().add(txtCurrentAverage[i], gbc_txtCurrentAverage);
			txtCurrentAverage[i].setColumns(10);
			
			txtRecencyAverage[i] = new JTextField();
			txtRecencyAverage[i].setEditable(false);
			txtRecencyAverage[i].setText(String.format("%1.3f", objective.recencyAvg));
			GridBagConstraints gbc_txtRecencyAverage = new GridBagConstraints();
			gbc_txtRecencyAverage.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtRecencyAverage.insets = new Insets(0, 0, 0, 5);
			gbc_txtRecencyAverage.gridx = 3;
			gbc_txtRecencyAverage.gridy = i+1;
			frame.getContentPane().add(txtRecencyAverage[i], gbc_txtRecencyAverage);
			txtRecencyAverage[i].setColumns(10);
			
			txtRecord[i] = new JTextField();
			txtRecord[i].setEditable(false);
			txtRecord[i].setText(String.format("%1.3f", objective.record));
			GridBagConstraints gbc_txtRecord = new GridBagConstraints();
			gbc_txtRecord.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtRecord.gridx = 4;
			gbc_txtRecord.gridy = i+1;
			frame.getContentPane().add(txtRecord[i], gbc_txtRecord);
			txtRecord[i].setColumns(10);
			
			txtFinalGoal[i] = new JTextField();
			txtFinalGoal[i].setEditable(false);
			txtFinalGoal[i].setText(String.format("%1.3f", objective.finalGoal));
			GridBagConstraints gbc_txtFinalGoal = new GridBagConstraints();
			gbc_txtFinalGoal.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtFinalGoal.gridx = 5;
			gbc_txtFinalGoal.gridy = i+1;
			frame.getContentPane().add(txtFinalGoal[i], gbc_txtFinalGoal);
			txtFinalGoal[i].setColumns(10);
			
			chkbxIsOn[i] = new JCheckBox();
			chkbxIsOn[i].setEnabled(false);
			chkbxIsOn[i].setSelected(objective.isOn);
			GridBagConstraints gbc_chkbxIsOn = new GridBagConstraints();
			gbc_chkbxIsOn.fill = GridBagConstraints.HORIZONTAL;
			gbc_chkbxIsOn.gridx = 6;
			gbc_chkbxIsOn.gridy = i+1;
			frame.getContentPane().add(chkbxIsOn[i], gbc_chkbxIsOn);
			txtFinalGoal[i].setColumns(10);
		}
	}

//	public void addObjectiveRow(Objective objective) {
//		objectiveCount++;
//		
//		JLabel lblObjectiveName = new JLabel(objective.name);
//		GridBagConstraints gbc_lblObjective = new GridBagConstraints();
//		gbc_lblObjective.fill = GridBagConstraints.HORIZONTAL;
//		gbc_lblObjective.insets = new Insets(0, 0, 0, 5);
//		gbc_lblObjective.gridx = 0;
//		gbc_lblObjective.gridy = objectiveCount;
//		frame.getContentPane().add(lblObjectiveName, gbc_lblObjective);
//		
//		JTextField txtCurrentGoal = new JTextField();
//		txtCurrentGoal.setEditable(false);
//		txtCurrentGoal.setText(String.format("%1.3f", objective.goal));
//		GridBagConstraints gbc_txtCurrentGoal = new GridBagConstraints();
//		gbc_txtCurrentGoal.fill = GridBagConstraints.HORIZONTAL;
//		gbc_txtCurrentGoal.insets = new Insets(0, 0, 0, 5);
//		gbc_txtCurrentGoal.gridx = 1;
//		gbc_txtCurrentGoal.gridy = objectiveCount;
//		frame.getContentPane().add(txtCurrentGoal, gbc_txtCurrentGoal);
//		txtCurrentGoal.setColumns(10);
//		
//		JTextField txtCurrentAverage = new JTextField();
//		txtCurrentAverage.setEditable(false);
//		txtCurrentGoal.setText(String.format("%1.3f", objective.avg));
//		GridBagConstraints gbc_txtCurrentAverage = new GridBagConstraints();
//		gbc_txtCurrentAverage.fill = GridBagConstraints.HORIZONTAL;
//		gbc_txtCurrentAverage.insets = new Insets(0, 0, 0, 5);
//		gbc_txtCurrentAverage.gridx = 2;
//		gbc_txtCurrentAverage.gridy = objectiveCount;
//		frame.getContentPane().add(txtCurrentAverage, gbc_txtCurrentAverage);
//		txtCurrentAverage.setColumns(10);
//		
//		JTextField txtRecencyAverage = new JTextField();
//		txtRecencyAverage.setEditable(false);
//		txtCurrentGoal.setText(String.format("%1.3f", objective.recencyAvg));
//		GridBagConstraints gbc_txtRecencyAverage = new GridBagConstraints();
//		gbc_txtRecencyAverage.fill = GridBagConstraints.HORIZONTAL;
//		gbc_txtRecencyAverage.insets = new Insets(0, 0, 0, 5);
//		gbc_txtRecencyAverage.gridx = 3;
//		gbc_txtRecencyAverage.gridy = objectiveCount;
//		frame.getContentPane().add(txtRecencyAverage, gbc_txtRecencyAverage);
//		txtRecencyAverage.setColumns(10);
//		
//		JTextField txtFinalGoal = new JTextField();
//		txtFinalGoal.setEditable(false);
//		txtCurrentGoal.setText(String.format("%1.3f", objective.finalGoal));
//		GridBagConstraints gbc_txtFinalGoal = new GridBagConstraints();
//		gbc_txtFinalGoal.fill = GridBagConstraints.HORIZONTAL;
//		gbc_txtFinalGoal.gridx = 4;
//		gbc_txtFinalGoal.gridy = objectiveCount;
//		frame.getContentPane().add(txtFinalGoal, gbc_txtFinalGoal);
//		txtFinalGoal.setColumns(10);
//		
//		
//	}
	
	public void update() {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				for (int i = 0; i < objectives.size(); i++) {
					Objective o = objectives.get(i);
					txtCurrentGoal[i].setText(String.format("%1.3f", o.goal));
					txtCurrentAverage[i].setText(String.format("%1.3f", o.avg));
					txtRecencyAverage[i].setText(String.format("%1.3f", o.recencyAvg));
					txtRecord[i].setText(String.format("%1.3f", o.record));
					txtFinalGoal[i].setText(String.format("%1.3f", o.finalGoal));
					chkbxIsOn[i].setSelected(o.isOn);
				}
			}
		});

	}
	
	public void showWindow() {frame.setVisible(true);}
}
